﻿using GalaSoft.MvvmLight;
using System;

namespace WPRD.Models
{
    /// <summary>
    /// 质量控制模型
    /// </summary>
    public class QualityControl : ObservableObject
    {
        #region Private Properties
        /// <summary>
        /// 数据行
        /// </summary>
        private string _dataLine;

        /// <summary>
        /// 区站号
        /// </summary>
        private string _stationNumber;

        /// <summary>
        /// 产品标识（ROBS：实时，HOBS：半小时，OOBS：一小时）
        /// </summary>
        private string _product;

        /// <summary>
        /// 采样高度
        /// </summary>
        private int _altitude;

        /// <summary>
        /// 水平风向
        /// </summary>
        private Decimal _horizontalWindDirection;

        /// <summary>
        /// 水平风速
        /// </summary>
        private Decimal _horizontalWindVelocity;

        /// <summary>
        /// 垂直风速
        /// </summary>
        private Decimal _verticalWindVelocity;

        /// <summary>
        /// 水平方向可信度
        /// </summary>
        private int _horizontalCredibility;

        /// <summary>
        /// 垂直方向可信度
        /// </summary>
        private int _verticalCredibility;

        /// <summary>
        /// 质量代码
        /// </summary>
        private int _qualityCode;

        /// <summary>
        /// 质量信息
        /// </summary>
        private string _qualityInfo;
        #endregion

        #region Public Properties
        /// <summary>
        /// 数据行
        /// </summary>
        public string DataLine
        {
            get
            {
                return _dataLine;
            }
            set
            {
                if (value == _dataLine)
                    return;
                _dataLine = value;
                RaisePropertyChanged("DataLine");
            }
        }

        /// <summary>
        /// 区站号
        /// </summary>
        public string StationNumber
        {
            get
            {
                return _stationNumber;
            }
            set
            {
                if (value == _stationNumber)
                    return;
                _stationNumber = value;
                RaisePropertyChanged("StationNumber");
            }
        }

        /// <summary>
        /// 产品标识（ROBS：实时，HOBS：半小时，OOBS：一小时）
        /// </summary>
        public string Product
        {
            get
            {
                return _product;
            }
            set
            {
                if (value == _product)
                    return;
                _product = value;
                RaisePropertyChanged("Product");
            }
        }

        /// <summary>
        /// 气压高度
        /// </summary>
        public int Altitude
        {
            get
            {
                return _altitude;
            }
            set
            {
                if (value == _altitude)
                    return;
                _altitude = value;
                RaisePropertyChanged("Altitude");
            }
        }

        /// <summary>
        /// 水平风向
        /// </summary>
        public Decimal HorizontalWindDirection
        {
            get
            {
                return _horizontalWindDirection;
            }
            set
            {
                if (value == _horizontalWindDirection)
                    return;
                _horizontalWindDirection = value;
                RaisePropertyChanged("HorizontalWindDirection");
            }
        }

        /// <summary>
        /// 水平风速
        /// </summary>
        public Decimal HorizontalWindVelocity
        {
            get
            {
                return _horizontalWindVelocity;
            }
            set
            {
                if (value == _horizontalWindVelocity)
                    return;
                _horizontalWindVelocity = value;
                RaisePropertyChanged("HorizontalWindVelocity");
            }
        }

        /// <summary>
        /// 垂直风速
        /// </summary>
        public Decimal VerticalWindVelocity
        {
            get
            {
                return _verticalWindVelocity;
            }
            set
            {
                if (value == _verticalWindVelocity)
                    return;
                _verticalWindVelocity = value;
                RaisePropertyChanged("VerticalWindVelocity");
            }
        }

        /// <summary>
        /// 水平方向可信度
        /// </summary>
        public int HorizontalCredibility
        {
            get
            {
                return _horizontalCredibility;
            }
            set
            {
                if (value == _horizontalCredibility)
                    return;
                _horizontalCredibility = value;
                RaisePropertyChanged("HorizontalCredibility");
            }
        }

        /// <summary>
        /// 垂直方向可信度
        /// </summary>
        public int VerticalCredibility
        {
            get
            {
                return _verticalCredibility;
            }
            set
            {
                if (value == _verticalCredibility)
                    return;
                _verticalCredibility = value;
                RaisePropertyChanged("VerticalCredibility");
            }
        }

        /// <summary>
        /// 质量代码
        /// </summary>
        public int QualityCode
        {
            get
            {
                return _qualityCode;
            }
            set
            {
                if (value == _qualityCode)
                    return;
                _qualityCode = value;
                RaisePropertyChanged("QualityCode");
            }
        }

        /// <summary>
        /// 质量信息
        /// </summary>
        public string QualityInfo
        {
            get
            {
                return _qualityInfo;
            }
            set
            {
                if (value == _qualityInfo)
                    return;
                _qualityInfo = value;
                RaisePropertyChanged("QualityInfo");
            }
        }
        #endregion
    }

    public static class QualityControlExtension
    {
        public static string[] PropertyNames = { "采样高度", "水平风向", "水平风速", "垂直风速", "水平方向可信度", "垂直方向可信度", "垂直方向Cn2" };
        public static int[] PropertyLengths = { 5, 5, 5, 6, 3, 3, 8 };

        /// <summary>
        /// 解析数据行
        /// </summary>
        /// <param name="qualityControl">质量控制</param>
        public static void AnalysisQualityControl(this QualityControl qualityControl)
        {
            try
            {
                string[] properties = qualityControl.DataLine.Split(' ');
                if (properties.Length != 7)
                    throw new Exception("数据行应为字符‘ ’分隔的7段");
                for (int i = 0; i < properties.Length; i++)
                {
                    string property = properties[i];
                    if (property.Length != PropertyLengths[i])
                        throw new Exception(String.Format("数据项{0}长度应为{1}字节", PropertyNames[i], PropertyLengths[i]));
                    if (property.Trim('/') == "")
                    {
                        qualityControl.QualityCode = 8;
                        qualityControl.QualityInfo = String.Format("数据项{0}缺测", PropertyNames[i]);
                    }
                    else
                    {
                        switch (i)
                        {
                            case 0:
                                qualityControl.Altitude = Convert.ToInt32(properties[0]);
                                break;
                            case 1:
                                qualityControl.HorizontalWindDirection = Convert.ToDecimal(properties[1]);
                                break;
                            case 2:
                                qualityControl.HorizontalWindVelocity = Convert.ToDecimal(properties[2]);
                                break;
                            case 3:
                                qualityControl.VerticalWindVelocity = Convert.ToDecimal(properties[3]);
                                break;
                            case 4:
                                qualityControl.HorizontalCredibility = Convert.ToInt32(properties[4]);
                                break;
                            case 5:
                                qualityControl.VerticalCredibility = Convert.ToInt32(properties[5]);
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                qualityControl.QualityCode = 2;
                qualityControl.QualityInfo = String.Format("数据行格式错误\n{0}", ex.Message);
            }
        }
    }
}
